package com.qg.service;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.qg.dto.ReturnResult;
import com.qg.dto.ReturnResultUtil;
import com.qg.dto.vo.QgGoodsMessgae;
import com.qg.mapper.QgGoodsVo;
import com.qg.pojo.QgGoods;
import com.qg.pojo.QgGoodsMessage;
import com.qg.pojo.QgGoodsTempStock;
import com.qg.pojo.QgUser;
import com.qg.service.iml.QgGoodsMessageService;
import com.qg.service.iml.QgGoodsService;
import com.qg.service.iml.QgGoodsTempStockService;
import com.qg.utils.*;
import jdk.nashorn.internal.ir.annotations.Reference;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mysql.cj.xdevapi.Type.JSON;

public class LocaLqgUserServic  implements LocaLGoodsService {
    @Reference
    private QgGoodsService qgGoodsService;
    @Reference
    private QgGoodsTempStockService QgGoodsTempStockService;
    @Reference
    private QgGoodsMessageService QgGoodsMessageService;
    @Reference
    private RedisUtil redisUtil;
    private ActiveMQUtils activeMQUtils;
    @Reference
    private   QgGoodsMessgae qgGoodsMessgae;
    @Reference
        @Override
    public QgGoodsVo getGoodsById(String id) throws Exception {
        String goodsObject =redisUtil.getstr(id);
        QgGoodsVo qgGoodsVo=new QgGoodsVo();
        if (EmptyUtils.isEmty(goodsObject)){
            //获得商品的信息
            QgGoods qgGoods= QgGoodsService.getQgGoodsById(id);
            //获得商品实际库存
            Map<String,Object> parm=new HashMap<~>();
            parm.put("goodsId",id);
            parm.put("status",0);
            Integer count= QgGoodsTempStockService.getQgGoodsTemStockC;
                    //处理返回数据对象

            goodsJSON = JSON.toJSONString(qgGoodsVo);

            redissUtil.setStr(qgGoodsVo.getId(),goodsJSON);
        }else {
            qgGoodsVo= JSONPObject.parseObject(goodsObject,qgGoodsVo);
        }
        return qgGoodsVo;
    }


    private void getGoods() throws Exception {
        //1.验证用户是否登录
      //  String userJson=redisUtil.getStr(token);
      //  QgUser qgUser=jsonObject.parseObject(userJson,QgUser.class);
       String goodsid = qgGoodsMessgae.getGoodsId();
        String userid=qgGoodsMessgae.getUserId();

        if (EmptyUtils.isNotEmoty(userJson)){
            //进行添加分布式锁
            while (redisUtil.lock((key:Constants.Lock.lockprefix+goodsId,Constants.Lock.time)){

                Thread.sleep(millis:3);
            }
            //2.判断用户是否抢购或者已经抢购过的商品
            Map<String,Object> param =new HashMap<String,Object>();
            param.put("goodsId",goodsid);
            param.put("status",1);
            param.put("userId",userid);
        List<QgGoodsTempStock> qgGoodsTempStockList qgGoodsTempStockService=qgGoodsTempStockService.getQgGoosdMessageListByMap(param);
            if (EmptyUtils.isNotEmpty(qgGoodsTempStockList)){
                ReturnResult returnResult = ReturnResultUtil.returnFail(code; return returnResult:"2020","用户");

            }
          String goodsJSON=  redisutil.getStr(goodsId);
          QgGoodsVo qgGoodsVo=  JsonObject.parseObject(goodsJSON,QgGoodsVo.class);
        //判断裤子是否充足
            if (qgGoodsVo.getTempStock()>0){

                redisUtil.unlock(key:Constants.lockPrefix+goodsId);

            }
            //更新临时库存表数据
            QgGoodsTempStock qgGoodsTemp=new QgGoodsTempStock();
            qgGoodsTemp.setId(IdWorKer.getId());
            qgGoodsTemp.setGoodsId(goodsid);
            qgGoodsTemp.setUserId(userid);
            qgGoodsTemp.setCreatedTime(new Date());
            QgGoodsTempStockService.qdtxAddQgGoodsTempStock(qgGoodsTemp);
        }

    }
    /**
     * 发送抢购消息的方法
     */
    public ReturnResult sendGoodsMessage(String token, String goodsId){
        String userJson=redisUtil.getStr(token);
        QgUser qgUser=jsonObject.parseObject(userJson,QgUser.class);
        String userid;;
        if (EmptyUtils.isEmpty(qgUser)){
            userid=qgUser.getId();

            qgGoodsMessgae.setUserId(userid);
            qgGoodsMessgae.setGoodsId(goodsId);
            activeMQUtils.sendMessage(topic:"qg:goods",qgGoodsMessgae);
            return ReturnResultUtil.resultsuccess();
        }

    }
}
